define(window.backboneCommon.getViewDefine("spread.detail"), function ($,require,Collection,tpl) {
    var defaultsOptions={
        campaignId:"",
        submitCallback:null
    };
    var pagePrivate = {
        getDefaults :function () {
            return defaultsOptions
        },
        getOptions : function (options) {
            options=$.ExtCommon.isCheckEmpty(options)?{}:options;
            return $.ExtCommon.initMergeJson(this.getDefaults(),options);
        }
    };
    var backboneView= Backbone.View.extend({
        events: {
            "click #btn_submitSpread": "saveSubmit"
        },
        initialize: function () {
            this.modelAttach={};
            this.modelAttach.baseData=this.model.get("modelBaseData");
            this.listenTo(this.modelAttach.baseData, 'bind:SpreadType', this.bindSpreadType);
            this.listenTo(this.modelAttach.baseData, "bind:ObjMatterType", this.bindObjMatterType);
            this.listenTo(this.model, 'bind:Info', this.bindInfo);
            this.listenTo(this.model, 'save:Info', this.saveInfoCallback);
            this.render();
        },
        render:function(){
            var templateHtml=_.template(tpl);
            this.$el.html(templateHtml());
            return this;
        },
        initMain:function(options){
            this.elementModel={
                modal:$('#modal_detailsSpread'),
                form:$('#form_detailsSpread'),
                dayBudget:$("#txt_dayBudget")
            };
            this.extObject={};
            this.pageData={initPage:false};
            this.pageOptions=$.ExtCommon.initMergeJson(pagePrivate.getOptions(),options);
            this.elementModel.form.ExtParsley();
            this._initPagePower();
        },
        _initPagePower:function(){
            var self=this;
            $.ExtAssist.rolePower.operatePage("select",["edit"],function(json){
                self.pageData.power={
                    isOnlyView:json["select"],
                    isEdit:json["edit"]
                };
                if(self.pageData.power.isEdit){self.pageData.power.isOnlyView=false;}
            });
        },
        initPage:function (id) {
            this.collection.getInfo({campaignId:this.pageOptions.campaignId,id:id});
        },
        bindInfo:function () {
            var self=this;
            var result=this.model.get("dataInfo");
            if(result.res.success){
                self.pageData.isAdd=result.isAdd;
                var operateName="";
                if(self.pageData.isAdd){operateName="新建"}
                else{operateName=self.pageData.power.isOnlyView?"查看":"编辑"}
                self.elementModel.modal.find(".modal-title").text($.ExtCommon.getStringConnect("{0}推广计划",operateName));
                self.extObject.formValue=self.elementModel.form.ExtFormValue({isRunBind:false,
                    initCallback:function(dataJson) {
                        self.pageData.spreadInfo = dataJson;
                        if(!self.pageData.initPage) {
                            self.pageData.initPage = true;
                            self.collection.initAfterDetails();
                        }
                        self.elementModel.form.find(":text,textarea").prop("disabled", self.pageData.power.isOnlyView);
                        self._changeSpreadType(dataJson);
                        self._replaceObjMatterType(dataJson.objMatterType.toString());
                        self._disabledObjMatterType();
                        self.elementModel.dayBudget.ExtNumeric().refresh();
                        self.elementModel.form.ExtParsley("reset");
                        self._showModal();
                    }
                });
                self.extObject.formValue.cleanValue();
                self.extObject.formValue.bindValueData(result.res.data);
            }else{
                $.ExtNotify.error("该推广计划数据获取失败：" +result.res.msg)
            }
        },

        bindSpreadType:function () {
            var self=this;
            var res=this.modelAttach.baseData.get("dataSpreadType");
            this.extObject.spreadType=$("#panel_spreadType").ExtRadioCheckbox({type: "radio",attrName:"spreadType",data: res.data,
                initCallback:function () {self._changeSpreadType()},
                changeCallback:function () {
                    self._changeSpreadType();
                    self._replaceObjMatterType();
                }
            });
        },
        _changeSpreadType:function (info) {
            info= info||this.extObject.spreadType.getValueInfo();
            if(info.isOrdinary){
                this.elementModel.dayBudget.attr({required:"","data-parsley-numrange":"[50,4000000]"}).parent().prev().show();
            }else if(info.isWeChat){
                this.elementModel.dayBudget.removeAttr("required data-parsley-numrange").parent().prev().hide();
            }
            this.elementModel.form.ExtParsley().validate(this.elementModel.dayBudget);
            if(this.extObject.spreadType){
                this.pageData.isAdd?this.extObject.spreadType.cleanDisabledAll():this.extObject.spreadType.disabledAll()
            }
        },
        bindObjMatterType:function () {
            var self=this;
            var res=self.modelAttach.baseData.get("dataObjMatterType");
            self.pageData.objMatterType=res.data;
            var list=self.pageData.objMatterType.group[self.pageData.spreadInfo.spreadType];
            self.extObject.objMatterType=$("#panel_objMatterType").ExtRadioCheckbox({type: "radio",attrName:"objMatterType",data: list,
                initCallback:function () {self._disabledObjMatterType()}
            });
        },
        _replaceObjMatterType:function (initValue) {
            if(this.extObject.objMatterType) {
                var spreadTypeInfo = this.extObject.spreadType.getValueInfo();
                var data = this.pageData.objMatterType.group[spreadTypeInfo.id];
                this.extObject.objMatterType.replaceData(data);
                this.extObject.objMatterType.setValue($.ExtCommon.isCheckEmpty(initValue)?0:initValue);
            }
        },
        _disabledObjMatterType:function () {
            if(this.extObject.objMatterType){
                this.pageData.isAdd?this.extObject.objMatterType.cleanDisabledAll():this.extObject.objMatterType.disabledAll()
            }
        },

        _checkForm:function () {
            return this.elementModel.form.ExtParsley("validate");
        },
        saveSubmit:function () {
            if(this._checkForm()){
                var ajaxData = this.elementModel.form.ExtFormValue("getValues");
                ajaxData.campaignId= this.pageOptions.campaignId;
                ajaxData.spreadTypeInfo=this.extObject.spreadType.getValueInfo();
                ajaxData.isWeChat=ajaxData.spreadTypeInfo.isWeChat;
                var infoOriginal = this.elementModel.form.ExtFormValue("getOriginalData");
                if(infoOriginal.id){ajaxData.id=infoOriginal.id}
                this._hideModal();
                this.collection.saveInfo(ajaxData);
            }
        },
        saveInfoCallback:function () {
            var result=this.model.get("dataSave");
            if (result.res.success) {
                $.ExtNotify.success("保存成功!");
                if(this.pageOptions.submitCallback){this.pageOptions.submitCallback(result.isAdd,result.req)}
            }else{
                this._showModal();
                $.ExtNotify.error(result.res.msg);
            }
        },
        _showModal:function(){
            this.elementModel.modal.modal("show");
            $.ExtAssist.modal.computeTop(this.elementModel.modal);
        },
        _hideModal: function () {
            this.elementModel.modal.modal("hide");
        }
    });
    var viewModel=function(options){
        this.collection= new Collection();
        this.model=this.collection.model;
        this.view=new backboneView({el:options.pElement,collection:this.collection,model:this.model});
        this.view.initMain(options);
    };
    viewModel.prototype={
        init:function (id) {
            this.view.initPage(id);
        }
    };
    return viewModel;
});